package in.co.codedoc.sql.mapping;

import in.co.codedoc.cg.annotations.DBType;
import in.co.codedoc.cg.annotations.IsADBOutputColumnMap;
import in.co.codedoc.sql.DBOutputColumnMap;
import in.co.codedoc.time.DayTime;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.GregorianCalendar;

@IsADBOutputColumnMap(javaType=DayTime.class,dbType=DBType.TIMESTAMP)
public class TimestampDayTimeOutputColumnMap
	extends DBOutputColumnMap
	implements ObjectOutputColumnMap
{

	@Override
	public void ReadColumn(int index, ResultSet rs)
		throws SQLException
	{
		Timestamp ts = rs.getTimestamp(index);
		
		if(rs.wasNull())
		{
			SetWasNull();
		}
		else
		{
			Calendar cal = new GregorianCalendar();
			
			cal.setTimeInMillis(ts.getTime());
			
			daytime = new DayTime(cal);
		}
	}

	@Override
	public Object GetValue()
	{
		return daytime;
	}
	
	private DayTime daytime;
}
